<template>
  <el-dialog  class="dia-style" :title="title" :visible.sync="open" width="60%" append-to-body>
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="formData.roleName" placeholder="请输入角色名称"/>
      </el-form-item>
      <el-form-item prop="roleKey">
          <span slot="label">
            <el-tooltip content="控制器中定义的权限字符，如：@PreAuthorize(`@ss.hasRole('admin')`)" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            权限字符
          </span>
        <el-input v-model="formData.roleKey" placeholder="请输入权限字符"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, updateRole } from '@/api/system/role'

export default {
  name: 'editProduct',
  props:{
    title:{
      type:String,
      default(){
        return '编辑'
      }
    },
    formData:{
      type:Object,
      default(){
        return {}
      }
    },
  },
  data(){
    return {
      isOpen:this.open,
      // 表单参数
      rules: {
        roleName: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        roleKey: [
          { required: true, message: '权限字符不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch:{
    isOpen:{
      handler(newValue){
        this.isOpen=newValue
      }
    }
  },
  methods:{
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.formData.roleId != undefined) {
            updateRole(this.formData).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.isOpen = false
              this.getList()
            })
          } else {
            addRole(this.formData).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.isOpen = false
              this.getList()
            })
          }
        }
      })
    },
    // 取消按钮
    cancel(){
      this.isOpen=false
    },
  }
}
</script>

<style scoped>

</style>
